package uiLayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import errorLayer.*;
import modelLayer.Room;
import controlLayer.RoomCtrl;

public class RoomGUI extends JPanel {
	
	private JTextField roomNameField = new JTextField();
	private JTextField roomIDField = new JTextField();
	private JComboBox allRoomsBox = new JComboBox();
	private List<Room> allRooms = new ArrayList<>();
	private RoomCtrl rc = new RoomCtrl();
	private JButton addBtn = new JButton("Add");
	private JButton editBtn = new JButton("Edit");
	private JButton removeBtn = new JButton("Remove");
	
	public RoomGUI () {
		
		setLayout(new BorderLayout(0, 0));
		
		allRooms = rc.allRooms();
		
		setup();
		
	}
	
	private void setup() {
		
		JPanel roomContent = new JPanel();
		roomContent.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), "Room"));
		roomContent.setLayout(new BorderLayout(0, 0));
		add(roomContent, BorderLayout.NORTH);
		
		JPanel textNameArea = new JPanel();
		textNameArea.setLayout(new GridLayout(4, 1, 0, 5));
		roomContent.add(textNameArea, BorderLayout.WEST);
		
		JPanel textArea = new JPanel();
		textArea.setBorder(new EmptyBorder(5, 5, 5, 5));
		textArea.setLayout(new GridLayout(4, 1, 0, 5));
		roomContent.add(textArea, BorderLayout.CENTER);
		
		JPanel buttonArea = new JPanel();
		buttonArea.setBorder(new EmptyBorder(5, 5, 5, 5));
		buttonArea.setLayout(new GridLayout(4, 1, 0, 5));
		roomContent.add(buttonArea, BorderLayout.EAST);
		
		roomIDField.setEditable(false);
		
		allRoomsBox.addItem("New room");
		
		if (allRooms.size() == 0) {
			
			allRoomsBox.addItem("No rooms");
			
		}
		
		allRoomsBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				
				if (e.getItem() == "New room") {
					
					addBtn.setEnabled(true);
					editBtn.setEnabled(false);
					removeBtn.setEnabled(false);
					
					roomIDField.setText("");
					roomNameField.setText("");
					
				}
				else if (e.getItem() != "New room") {
					
					addBtn.setEnabled(false);
					editBtn.setEnabled(true);
					removeBtn.setEnabled(true);
					
					try {
						
						Room room = (Room) e.getItem();
						
						roomIDField.setText("" + room.getID());
						roomNameField.setText(room.getName());
						
					}
					catch (Exception ex) {
						System.out.println("Exception: " + ex);
					}
					
				}
				
			}
		});
		
		for (int i = 0; i < allRooms.size(); ++i) {
			
			allRoomsBox.addItem(allRooms.get(i));
			
		}
		
		textNameArea.add(new JLabel("ID"));
		textNameArea.add(new JLabel("Name"));
		
		textArea.add(roomIDField);
		textArea.add(roomNameField);
		
		buttonArea.add(allRoomsBox);
		
		addBtn.setPreferredSize(new Dimension(150, 20));
		addBtn.setMinimumSize(new Dimension(150, 20));
		addBtn.setMaximumSize(new Dimension(150, 20));
		
		addBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				Room room = new Room();
				
				if (!roomNameField.getText().isEmpty()) {
					
					room.setName(roomNameField.getText());
					
					rc.addRoom(room);
					if (allRoomsBox.getItemAt(1) == "No rooms") {
						allRoomsBox.remove(1);
					}
					
					room.setID(rc.getHighestID());
					
					allRoomsBox.addItem(room);
					allRoomsBox.setSelectedIndex((allRoomsBox.getItemCount() - 1));
					
				}
				else {
					new EmptyField("Room name");
				}
				
			}
		});
		
		buttonArea.add(addBtn);
		
		editBtn.setPreferredSize(new Dimension(150, 20));
		editBtn.setMinimumSize(new Dimension(150, 20));
		editBtn.setMaximumSize(new Dimension(150, 20));
		editBtn.setEnabled(false);
		
		editBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				Room room = new Room();
				
				if (!roomIDField.getText().isEmpty()) {
					
					room.setID(Integer.parseInt(roomIDField.getText()));
					room.setName(roomNameField.getText());
					
					rc.updateRoom(room);
					
					allRoomsBox.removeItemAt(allRoomsBox.getSelectedIndex());
					allRoomsBox.addItem(room);
					
				}
				
			}
		});
		
		buttonArea.add(editBtn);
		
		removeBtn.setPreferredSize(new Dimension(150, 20));
		removeBtn.setMinimumSize(new Dimension(150, 20));
		removeBtn.setMaximumSize(new Dimension(150, 20));
		removeBtn.setEnabled(false);
		
		removeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				
				String deletion = "Room: " + roomIDField.getText() + ", " + roomNameField.getText();
				
				DeleteWarning dw = new DeleteWarning(deletion);
				
				if(dw.getAnswer()) {
					rc.removeRoom(Integer.parseInt(roomIDField.getText()));
				
					allRoomsBox.removeItemAt(allRoomsBox.getSelectedIndex());
				}
				
			}
		});
		
		buttonArea.add(removeBtn);
		
	}

}
